package cn.csc.module.shop.controller.admin.user;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.shop.controller.admin.user.vo.*;
import cn.csc.module.shop.convert.user.ShopUserConvert;
import cn.csc.module.shop.dal.dataobject.user.ShopUserDO;
import cn.csc.module.shop.service.user.ShopUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 商家用户")
@RestController
@RequestMapping("/shop/user")
@Validated
public class ShopUserController {

    @Resource
    private ShopUserService userService;

    @PostMapping("/create")
    @ApiOperation("创建商家用户")
    @PreAuthorize("@ss.hasPermission('shop:user:create')")
    public CommonResult<BigInteger> createUser(@Valid @RequestBody ShopUserCreateReqVO createReqVO) {
        return success(userService.createUser(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新商家用户")
    @PreAuthorize("@ss.hasPermission('shop:user:update')")
    public CommonResult<Boolean> updateUser(@Valid @RequestBody ShopUserUpdateReqVO updateReqVO) {
        userService.updateUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除商家用户")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shop:user:delete')")
    public CommonResult<Boolean> deleteUser(@RequestParam("id") BigInteger id) {
        userService.deleteUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得商家用户")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('shop:user:query')")
    public CommonResult<ShopUserRespVO> getUser(@RequestParam("id") BigInteger id) {
        ShopUserDO user = userService.getUser(id);
        return success(ShopUserConvert.INSTANCE.convert(user));
    }

    @GetMapping("/list")
    @ApiOperation("获得商家用户列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('shop:user:query')")
    public CommonResult<List<ShopUserRespVO>> getUserList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<ShopUserDO> list = userService.getUserList(ids);
        return success(ShopUserConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得商家用户分页")
    @PreAuthorize("@ss.hasPermission('shop:user:query')")
    public CommonResult<PageResult<ShopUserRespVO>> getUserPage(@Valid ShopUserPageReqVO pageVO) {
        PageResult<ShopUserDO> pageResult = userService.getUserPage(pageVO);
        return success(ShopUserConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出商家用户 Excel")
    @PreAuthorize("@ss.hasPermission('shop:user:export')")
    @OperateLog(type = EXPORT)
    public void exportUserExcel(@Valid ShopUserExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ShopUserDO> list = userService.getUserList(exportReqVO);
        // 导出 Excel
        List<ShopUserExcelVO> datas = ShopUserConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "商家用户.xls", "数据", ShopUserExcelVO.class, datas);
    }

}
